<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>企业之家角色管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="${global_templets_skin}/systemstyle/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="${global_templets_skin}/systemstyle/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div class="layui-form-item">
        <div class="layui-inline">
          角色筛选
        </div>
        <div class="layui-inline">
          <select name="rolename" lay-filter="LAY-user-adminrole-type">
            <option value="-1">全部角色</option>
            <option value="0">管理员</option>
            <option value="1">超级管理员</option>
            <option value="5">运营人员</option>
            <option value="6">编辑</option>
          </select>
        </div>
      </div>
    </div>
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-role" data-type="batchdel">删除</button>
        <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
      </div>

      <table id="LAY-user-back-role" lay-filter="LAY-user-back-role"></table>
      <script type="text/html" id="table-role-status">
        {{#  if(d.status == true){ }}
        <button class="layui-btn layui-btn-xs">启用</button>
        {{#  } else { }}
        <button class="layui-btn layui-btn-primary layui-btn-xs">禁用</button>
        {{#  } }}
      </script>
      <script type="text/html" id="table-role-setup">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        {{#  if(d.role == '超级管理员'){ }}
        <a class="layui-btn layui-btn-disabled layui-btn-xs"><i class="layui-icon layui-icon-delete"></i>删除</a>
        {{#  } else { }}
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        {{#  } }}
      </script>
    </div>
  </div>
</div>

<script src="${global_templets_skin}/systemstyle/layui/layui.js"></script>
<script>
    layui.config({
        base: '${global_templets_skin}/systemstyle/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index',  'table'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;

        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data){
            var field = data.field;

            //执行重载
            table.reload('LAY-user-manage', {
                where: field
            });
        });

        //方法级渲染
        table.render({
            elem: '#LAY-user-back-role'
            ,url: '/admin/role/user_role_listData.do'
            ,cols: [[
                {checkbox: true, fixed: true}
                ,{field:'id', title: 'ID', width:180, sort: true, fixed: true}
                ,{field:'roleName', title: '角色名', width:180}
                ,{field:'remark', title: '备注', sort: true, width:280}
                ,{field:'sortOrder', align:'center', title: '排序', sort: true}
                ,{field:'status', title: '状态', align:'center', templet: '#table-role-status', unresize: true}
                ,{title:'操作', align:'center', templet: '#table-role-setup', unresize: true}
            ]]
            ,id: 'roleReload'
            ,page: true
        });

        //事件
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('roleReload')
                    ,checkData = checkStatus.data; //得到选中的数据
                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }
                deList=[];
                checkData.forEach(function(n,i){
                    deList.push(n.id);
                })
                layer.confirm('确定删除吗？', {
                    btn: ['确定','取消'] //按钮
                }, function() {
                    $.ajax({
                        type: "POST"
                        ,dataType: 'json'
                        , url: '/admin/role/del_user_role.do' //实际使用请改成服务端真实接口
                        , data: "ids=" + deList
                        , done: function (e) {
                            if (e.code == "0") {
                                layer.msg('成功');
                            }else {
                                layer.msg(e.message);
                                return;
                            }
                        }
                    });
                    table.reload('roleReload');
                    layer.msg('已删除', {icon: 1});
                });
            }
            ,add: function(){
                layer.open({
                    type: 2
                    ,title: '添加角色'
                    ,content: '/admin/role/add_user_role.do'
                    ,maxmin: true
                    ,area: ['500px', '450px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url:'/admin/role/add_user_role.do',
                                type:'post',
                                dataType: 'json',
                                data: data.field,
                                success:function(e){
                                    if (e.code == "0") {
                                        layer.msg('成功');
                                    }else {
                                        layer.msg(e.message);
                                        return;
                                    }
                                }
                            })
                            table.reload('roleReload'); //数据刷新
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-role').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听行工具事件
        table.on('tool(LAY-user-back-role)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除当前角色吗？', function(index){
                    $.ajax({
                        type: "POST"
                        ,dataType: 'json'
                        , url: '/admin/role/del_user_role.do' //实际使用请改成服务端真实接口
                        , data: "ids=" + data.id
                        , done: function (e) {
                            if (e.code == "0") {
                                layer.msg('成功');
                            }else {
                                layer.msg(e.message);
                                return;
                            }
                        }
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '修改角色'
                    ,content: '/admin/role/update_user_role.do?id='+data.id
                    ,maxmin: true
                    ,area: ['500px', '450px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url:'/admin/role/update_user_role.do',
                                type:'post',
                                dataType: 'json',
                                data: data.field,
                                success:function(e){
                                    if (e.code == "0") {
                                        layer.msg('成功');
                                    }else {
                                        layer.msg(e.message);
                                        return;
                                    }
                                }
                            })
                            table.reload('roleReload'); //数据刷新
                            layer.close(index); //关闭弹层
                        });
                        submit.trigger('click');
                    }
                });
            }
        });
    });
</script>
</body>
</html>
